#! /usr/bin/env python3.6

def plus(x,y):
    if not isinstance(x, (int, float)):
        raise TypeError('please input int or float number.')
    if not isinstance(y, (int, float)):
        raise TypeError('please input int or float number.')
    else:
        return x + y

# 有默认参数

#  *** 默认参数必须指向不变对象！***
def sort(x, y="hello"):
    temp = x
    x = y
    y = temp

    return x, y

print('3 + 2 = %s' % plus(3,2))
print('sort "a", "b": ', sort('a','b'))
print('sort "a", default argument: ', sort('a'))

# 可变参数 类似js的 ...args
def plus_update(*args):
    count = 0
    for num in args:
        count += num

    return count

print('test 1+2+3+4+5 = ', plus_update(1,2,3,4,5))

# 关键字参数 扩展func, 需要写参数名，在func内部包装成一个dict
def person(name, age, **args):
    print('name:', name, ' age:', age, ' args:', args)

person('zhangboping', 27, city='fuzhou')

# 简化写法 传入一个dict
person('zhangboping', 27, **{'city':'fuzhou','district':'JinAn'})

# 命名关键字参数 限制传入关键字参数的参数名, 在关键字参数前添加[*]
def person_strict(name, age, *, city, district):
    print('name:', name, ' age:', age, ' args:', {"city":city,"district":district})

person_strict('zhangboping', 20, city="fuzhou", district="JinAn")
person_strict('zhangboping', 21, **{'city':'fuzhou','district':'JinAn'})
# This is wrong.
# person_strict('zhangboping', 20, **{'city':'fuzhou','district':'JinAn', 'street':'HuaGong'})

# 如果命名关键字参数前有可变参数，则可以省略[*]
# 也可以设置默认值
def plus_update_plus(*nums, author="FreMaNgo"):
    count = 0
    for num in nums:
        count += num

    print('result is :', count, 'author: ', author)

plus_update_plus(1,2,3,4,5, **{'author': 'zhangboping'})
plus_update_plus(1,2,3,4,5)

# 递归函数 (=_=!) 没啥好说的 都一样